package com.camelot.boot.server;


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.stream.Collectors;

public class DocumentLoader {
    public String load(String filePath) throws IOException {
        if (filePath.endsWith(".docx")) {
            return parseDocx(filePath);
        } else if (filePath.endsWith(".pdf")) {
            return parsePdf(filePath);
        }
        throw new IllegalArgumentException("Unsupported file format");
    }

    private String parseDocx(String path) throws IOException {
        XWPFDocument doc = new XWPFDocument(new FileInputStream(path));
        return doc.getParagraphs().stream()
                .map(XWPFParagraph::getText)
                .collect(Collectors.joining("\n"));
    }

    private String parsePdf(String path) throws IOException {
        PDDocument document = null;
        PDFTextStripper stripper = new PDFTextStripper();
        return stripper.getText(document);
    }
}
